package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Zrj on 2015/9/14.
 */
public class MeReceivingAddressActivity extends BaseActivity {
    private CustomProgress customProgress;
    private EditText et_name, et_phone, et_mail, et_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_address);
        setTitle("收货地址");
        setNavBtn(R.mipmap.back, R.mipmap.bianji);

        findById();
    }

    private void findById() {

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_mail = (EditText) findViewById(R.id.et_mail);
        et_address = (EditText) findViewById(R.id.et_address);

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
            case RIGHT:
                startActivity(new Intent(AppContext.getContext(), MeReceivingAlterAddressActivity.class));
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mereceiving();

    }

    private void mereceiving() {
        customProgress = CustomProgress.show(MeReceivingAddressActivity.this, "请稍后....", false, null);

        JSONArray array = new JSONArray();
        final JSONObject object = new JSONObject();

        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");

        try {
            object.put("user_id", user_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8051", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {

                if (array == null) {
                    return;
                } else {


                    try {
                        JSONObject object1 = array.getJSONObject(0);

                        String address = object1.getString("address");
                        String consignees_name = object1.getString("consignees_name");
                        String consignees_phone = object1.getString("consignees_phone");
                        String postcode = object1.getString("postcode");


                        et_name.setText(consignees_name);
                        et_address.setText(address);
                        et_phone.setText(consignees_phone);
                        et_mail.setText(postcode);
                        SPUtils.put(AppContext.getContext(), "address", address);
                        SPUtils.put(AppContext.getContext(), "consignees_name", consignees_name);
                        SPUtils.put(AppContext.getContext(), "consignees_phone", consignees_phone);
                        SPUtils.put(AppContext.getContext(), "postcode", postcode);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }


                Log.w("code", code + "");
                Log.w("code", message + "");
                Log.w("code", array + "");
                customProgress.dismiss();
            }
        });


    }

}
